<template>
  <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">记账本登录</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="loginForm.user_name" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="loginForm.user_password" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember sky">记住密码</el-checkbox> | <router-link to="/register" class="sky margin-left-5">用户注册</router-link>
    <el-form-item style="width:100%;">
      <el-button type="primary" native-type='submit' style="width:100%;" @click.native.prevent="loginSubmit" :loading="logining">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  export default {
    data() {
      return {
        logining: false,
        loginForm: {
          user_name: null,
          user_password: null
        },
        rules: {
          user_name: [
            { required: true, message: '请输入账号', trigger: 'blur' },
          ],
          user_password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ]
        },
        checked: true
      };
    },
    methods: {
      loginSubmit(ev) {
        var _this = this;
        _this.$refs['loginForm'].validate((valid) => {
          if (valid) {
            _this.logining = true;
            _this.$http({
              method: 'POST',
              url: _this.config.baseUrl + 'user/login',
              data: _this.loginForm
            }).then(function (data) {
              var result = data.data;
              var response = result.code;
              _this.logining = false;
              if (response == 0) {
                if(result.isLogin){
                  _this.$router.push('/index/home');
                  localStorage.setItem('user',JSON.stringify(result.user))
                  }else {
                  _this.$message.error('用户名或密码错误！！');
                }
              } else {
                _this.$message.error('登录失败！！');
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    },
    mounted(){
        var loginUser=localStorage.getItem('user');
        if(loginUser&&loginUser.length>0){
            loginUser=JSON.parse(loginUser);
            this. loginForm= {
              user_name: loginUser.user_name,
              user_password: loginUser.user_password
            }
        }
    }
  }

</script>

<style >
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 10% auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

  }
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 5px 25px 0px;
  }
</style>
